package @{daoPkg};

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.zzwtec.information.dao.@{EntityModel}RowMapper;
import com.zzwtec.information.model.@{EntityModel};
import com.zzwtec.interfaces.Page;

<?
var date = date();
?>


/**
 * @{EntityModel}Dao测试类
 * @author 草原狼
 *
 * @{date,'yyyy年MM月dd日'}
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class @{EntityModel}DaoTest {
	@Autowired
	protected @{EntityModel}Dao dao;

	private final String TABLE_NAME = "@{entity_model}";
	
	@Before
	public void setup() {

	}
	
	@Test
	public void test_selectPage(){
		Page<@{EntityModel}> page = new Page<@{EntityModel}>();
		page.setPage(1);
		page.setPageNum(12);
		Page<@{EntityModel}> p = dao.selectPage(@{EntityModel}.class, TABLE_NAME, page, new @{EntityModel}RowMapper());
		assert (p.getData().size()  == 12);
	}
	
	@Test
	public void test_selectAll(){
		List<@{EntityModel}> list = dao.selectAll();
		assert (list.size() > 0);
	}
	
	@Test
	public void test_selectOne(){
		@{EntityModel} bean = dao.selectOne("ADJBUTFPmM==TODO");
		assert (bean != null);
	}
	
	@Test
	public void test_selectOneforBaseDao(){
		@{EntityModel} bean = dao.selectOne(TABLE_NAME, "ADJBUTFPmM==TODO", new @{EntityModel}RowMapper());
		assert (bean != null);
	}
	
	
	@Test
	public void test_updateSql(){
		@{EntityModel} bean = dao.selectOne("aOERiDCSvUITfU==TODO");
		String sql = "update @{entity_model}==TODO";
		int rows = dao.update(sql);
		assert (rows == 1);
	}
	
	@Test
	public void test_updateBean(){
		@{EntityModel} bean = dao.selectOne("aOERiDCSvUITfU==TODO");
		int rows = dao.update(bean);
		assert (rows == 1);
	}
	
	@Test
	public void test_batchUpdate(){
		@{EntityModel} bean = dao.selectOne("aOERiDCSvUITfU==TODO");
		String sql = "update @{entity_model} set name= ==TODO'";
		int[] rows = dao.batchUpdate(sql);
		assert (rows.length == 1);
	}
	
	
	
	@Test
	public void test_batchUpdateArgs(){
		@{EntityModel} bean = dao.selectOne("aOERiDCSvUITfU");
		String sql = " where id= ?";
		List<Object[]> batchArgs = new ArrayList<Object[]>();
		batchArgs.add(new Object[]{bean.getId()});
		int[] rows = dao.batchUpdate(sql, batchArgs);
		assert (rows.length == 1);
	}
	
	@Test
	public void test_insert(){
		@{EntityModel} bean = dao.selectOne("aqbhJRPjCUIuuZBPzweYBtzyNRrxg");
		dao.deleteById("aqbhJRPjCUIuuZBPzweYBtzyNRrxg");
		int rows = dao.insert(bean);		
		assert (rows == 1);
	}
	
	@Test
	public void test_deleteById(){
		@{EntityModel} bean = dao.selectOne("aqbhJRPjCUIuuZBPzweYBtzyNRrxg");
		int rows = dao.deleteById("aqbhJRPjCUIuuZBPzweYBtzyNRrxg");
		assert (rows == 1);
		dao.insert(bean);
	}
	
	@Test
	public void test_deleteByIds(){
		String[] ids = new String[]{"aYVzoTnajpuoGUdUtRIReE","BuakaaxAOOWanMCHlBpWwifkyNjkwfJF"};
		String sql = "SELECT * FROM " + TABLE_NAME + " WHERE in (?,?)";
		List<@{EntityModel}> list = dao.selectList(sql, ids, new @{EntityModel}RowMapper());
		int rows = dao.deleteByIds(ids);
		assert (rows == 2);
		for(@{EntityModel} bean : list){
			dao.insert(bean);
		}
	}	
	
	
}
